import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import {UserInfo, UserVO} from "@/api/system/system/user/types";


// 初始用户数据，可用于初始化
const initialUser: UserVO = {
    userId: '',
    tenantId: '',
    deptId: 0,
    userName: '',
    nickName: '',
    userType: '',
    email: '',
    phonenumber: '',
    sex: '',
    avatar: '',
    status: '',
    delFlag: '',
    loginIp: '',
    loginDate: '',
    remark: '',
    deptName: '',
    roles: [],
    roleIds: null,
    postIds: null,
    roleId: null,
    admin: false
}

const initState = {
    user: initialUser,
    permissions: [],
    roles: [],
    isSuperAdmin: false,
    accessToken: '',
    theme: <'light' | 'dark'>'light',
    followSystem: false,
    locale: 'zh-CN'
}

export const useUserStore = defineStore(
    'user',
    // Setup Store 写法，Vue3 推荐用这种方法写
    () => {
        const userInfo = ref({ ...initState })

        //设置登录token
        const setAccessToken = (token: string) => {
            userInfo.value.accessToken = token
        }

        const getAccessToken = () => {
            return userInfo.value.accessToken
        }

        //是否登录
        const isLogined = computed(() => {
            return !!userInfo.value.accessToken
        })

        //设置登录用户信息
        const setUserInfo = (val: UserInfo) => {
            userInfo.value.user = val.user
            userInfo.value.permissions = val.permissions
            userInfo.value.roles = val.roles
            userInfo.value.isSuperAdmin = val.roles.includes('superadmin')
        }

        //获取用户信息
        const getUserInfo = () => {
            return userInfo.value
        }

        // 清除用户信息，将用户信息重置为 initState 的副本
        const clearUserInfo = (): void => {
            userInfo.value = { ...initState }
        }

        // 计算属性，判断用户是否为超级管理员
        const isSuperAdmin = computed(() => {
            return userInfo.value.isSuperAdmin
        })

        // 计算属性，获取页面权限信息
        const getUserPermissionKeys = computed(() => {
            const permissionKeys: string[] = []
            // 如果用户已登录，添加 'logined' 权限
            if (isLogined.value) {
                permissionKeys.push('logined')
            }
            // 如果用户是超级管理员，添加 'superadmin' 权限
            if (isSuperAdmin.value) {
                permissionKeys.push('superadmin')
            }
            return permissionKeys
        })

        // 计算属性，获取用户按钮权限
        const getUserPermission = computed(() => {
            return userInfo.value.permissions
        })

        // 计算属性，获取主题状态
        const getTheme = computed(() => {
            return userInfo.value.theme
        })

        // 计算属性，获取 locale
        const getLocale = computed(() => {
            return userInfo.value.locale
        })

        // 设置 locale
        const setLocale = (newLocale: string): void => {
            userInfo.value.locale = newLocale
        }

        return {
            isSuperAdmin,
            isLogined,
            setLocale,
            getLocale,
            getTheme,
            getUserPermission,
            getUserPermissionKeys,
            setAccessToken,
            getAccessToken,
            setUserInfo,
            getUserInfo,
            clearUserInfo
        }
    },
    {
        persist: true // 是否持久化
    }
)
